package com.jiazhong.shopping.dao.impl;

import com.jiazhong.shopping.dao.CarDao;
import com.jiazhong.shopping.dao.DBUtil;
import com.jiazhong.shopping.model.Car;
import com.jiazhong.shopping.model.CarInfo;

import java.util.List;


public class CarDaoImpl extends DBUtil implements CarDao {
    @Override
    public boolean exists(int eat_id, int user_id) {
        String sql = "select * from tbl_car where eat_id=? and userId=?";
        Car car = super.executeQueryOne(sql,Car.class,eat_id,user_id);
        if(car!=null){
            return true;
        }
        return false;
    }

    @Override
    public void updateCarCount(Car car) {
        String sql = "update tbl_car set car_count=car_count+? where eat_id=? and userId=?";
        super.executeUpdate(sql,car.getCar_count(),car.getEat_id(),car.getUserId());
    }

    @Override
    public void updateCarCount(int car_id, int count) {
        String sql = "update tbl_car set car_count=? where car_id=?";
        super.executeUpdate(sql,count,car_id);
    }

    @Override
    public void addCar(Car car) {
        String sql = "insert into tbl_car values(default,?,?,?)";
        super.executeUpdate(sql,car.getEat_id(),car.getCar_count(),car.getUserId());
    }

    @Override
    public List<CarInfo> queryCarInfo(int userId ) {
        String sql="select car.car_id,eat.eat_id,eat.eat_name,eat.eat_price,eat.eat_image,car.car_count FROM tbl_eat eat INNER JOIN tbl_car car ON eat.eat_id=car.eat_id WHERE userId=?";
        return super.executeQueryList(sql,CarInfo.class,userId);
    }

    @Override
    public Integer queryCountByUserId(int userId) {
        String sql = "select count(*) from tbl_car where userId=?";
        return  ((Long)super.executeQueryNumber(sql,userId)).intValue();
    }

    @Override
    public void delCar(int car_id) {
        String sql = "delete from tbl_car where car_id=?";
        super.executeUpdate(sql,car_id);
    }

}

